package org.command.main.CS2;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 3000); // 连接服务器
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                System.out.println("个人通讯录管理：");
                System.out.println("1. 查看联系人信息");
                System.out.println("2. 添加新联系人");
                System.out.println("3. 修改联系人信息");
                System.out.println("4. 删除联系人");
                System.out.println("5. 退出");
                System.out.print("请选择操作: ");
                String choice = userInput.readLine();

                out.println(choice);

                if (choice.equals("1")) {
                    String data = in.readLine();
                    System.out.println("联系人信息:");
                    System.out.println(data);
                } else if (choice.equals("2")) {
                    System.out.print("请输入姓名: ");
                    String name = userInput.readLine();
                    System.out.print("请输入地址: ");
                    String address = userInput.readLine();
                    System.out.print("请输入电话: ");
                    String phone = userInput.readLine();
                    out.println(name);
                    out.println(address);
                    out.println(phone);
                    String response = in.readLine();
                    System.out.println(response);
                } else if (choice.equals("3")) {
                    System.out.print("请输入要修改的联系人姓名: ");
                    String name = userInput.readLine();
                    System.out.print("请输入新的地址: ");
                    String newAddress = userInput.readLine();
                    System.out.print("请输入新的电话: ");
                    String newPhone = userInput.readLine();
                    out.println(name);
                    out.println(newAddress);
                    out.println(newPhone);
                    String response = in.readLine();
                    System.out.println(response);
                } else if (choice.equals("4")) {
                    System.out.print("请输入要删除的联系人姓名: ");
                    String name = userInput.readLine();
                    out.println(name);
                    String response = in.readLine();
                    System.out.println(response);
                } else if (choice.equals("5")) {
                    socket.close();
                    break;
                } else {
                    System.out.println("无效选择，请重试.");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
